﻿(function () {
    //创建ADS命名空间
    if (!window.ADS) { window['ADS'] = {} }
    //检查浏览器是否与整个库兼容(使用能力检查而不是版本检测)
    function isCompatible(other) {
        if (other == false
        || !Array.prototype.push
        || !Object.hasOwnProperty
        || !document.createElement
        || !document.getElementsByTagName) {
            return false;
        }
        return true;
    };
    window['ADS']['isCompatible'] = isCompatible;
    /*---------------------------------------------------------------------*/

    //查找元素 （来自于prototype库）
    function $() {
        var elements = new Array();
        //查找作为参数提供的所有元素

        for (var i = 0; i < arguments.length; i++) {
            var element = arguments[i];
            if (typeof element == 'string') {
                element = document.getElementById(element);
            }
            if (arguments.length == 1) {
                return element
            }
            elements.push(element);
        }
        return elements;
    }
    window['ADS']['$'] = $;
    /*---------------------------------------------------------------------*/

    //添加（移除）事件监听，方便使用JS源文件和不唐突的方法为标记结构中的对象添加事件
    function addEvent(node, type, listener) {
        //检查兼容性以保证平稳退化
        if (!isCompatible()) { return false; }
        if (!(node == $(node)) || node == null) { return false; }
        if (node.addEventListener) {
            node.addEventListener(type, listener, false)//W3C方法
            return true;
        }
        else if (node.attachEvent) { //MSIE方法
            node['e' + type + listener] = listener;
            node[type + listener] = function () {
                node['e' + type + listener](window.event);
            }
            node.attachEvent('on' + type, node[type + listener]);
            return true;
        }
        return false;
    }
    window['ADS']['addEvent'] = addEvent;
    function removeEvent(node, type, listener) {
        if (!(node = $(node)) || node == null) { return false; }
        if (node.removeEventListener) {
            node.removeEventListener(type, listener, false); //W3C方法
            return true;

        }
        else if (node.detachEvent) {//MSIE方法
            node.detachEvent('on' + type, node[type + listener]);
            node[type + listener] = null;
            return true;
        }
        return false;
    };
    window['ADS']['removeEvent'] = removeEvent;

    /*---------------------------------------------------------------------*/
    //取消事件默认操作
    function preventDefault(eventObject) {
        eventObject = eventObject || getEventObject(eventObject);
        if (eventObject.preventDefault) {//W3C方式 
            eventObject.preventDefault();
        }
        else
            eventObject.returnValue = false; //MSIE
    }
    window['ADS']['preventDefault'] = preventDefault;
    function getEventObject(W3CEvent) {
        return W3CEvent || window.event;
    }
    //查找当前元素的下一个元素
    function nextElement(node, tag) {
        node = node.nextSibling;
        var aa = node.nodeName;
        var bb = tag.toUpperCase();
        if (node == null || node.nodeName == tag.toUpperCase()) {
            return node;
        }

        else {
            return nextElement(node, tag);
        }
    }
    window['ADS']['nextElement'] = nextElement;

    //根据类属性获取元素，和CSS样式相结合
    function getElementByClassName(cName, tag, parent) {
        tag = tag || '*';
        parent = parent || document;

        if (!(parent = $(parent))) { return false; }
        //查找所有匹配标签
        var allTags = (tag == '*' && !!parent.all) ? parent.all : parent.getElementsByTagName(tag);
        var matchingElements = new Array();

        //创建一个正则表达式，判断className是否正确
        cName = cName.replace(/\-/g, "\\-");
        var regex = new RegExp("(^|\\s)" + cName + "(\\s|$)");
        var element;
        for (var i = 0; i < allTags.length; i++) {
            element = allTags[i];
            if (regex.test(element.className)) {
                matchingElements.push(element);
            }
        }
        return matchingElements;
    };
    window['ADS']['getElementByClassName'] = getElementByClassName;


    //显示或隐藏一个元素
    function showElement() { }
    window['ADS']['showElement'] = showElement;
    //
    function insertAter() { }
    window['ADS']['insertAfter'] = insertAter;
    //
    function removeChildren() { }
    window['ADS']['removeChileren'] = removeChildren;
    //
    function prependChild() { }
    window['ADS']['prependChild'] = prependChild;



    //引入其它JS文件
    function LoadJS(src) {
        var headObj = document.getElementsByTagName('head')[0];
        scriptObj = document.createElement('script');
        scriptObj.language = 'javaScript';
        scriptObj.type = 'text/JavaScript';
        scriptObj.src = src;
        headObj.appendChild(scriptObj);
    }
    window['ADS']['LoadJS'] = LoadJS;
    //引入CSS文件
    function LoadCSS(src) {
        var headObj = document.getElementsByTagName('head')[0];
        cssObj = document.createElement('link');
        cssObj.setAttribute('rel', 'stylesheet');
        cssObj.setAttribute('type', 'text/css');
        cssObj.setAttribute('href', src);
        headObj.appendChild(cssObj);
    }
    window['ADS']['LoadCss'] = LoadCSS;


    //AJAX
    function getRequest(url, options) {
        //初始化请求对象
        var req = false;
        if (window.XMLHttpRequest) {
            req = new window.XMLHttpRequest();
        }
        else if (window.ActiveXObject) {
            req = new window.ActiveXObject('Microsoft.XMLHTTP');
        }
        if (!req) return false;
        //定义默认值
        options = options || {};
        options.method = options.method || 'GET';
        options.send = options.send || null;

        //定义各阶段的事件侦听器
        req.onreadystatechange = function () {
            switch (req.readyState) {
                case 1: //载入中
                    if (options.loadListener) {
                        options.loadListener.apply(req, arguments);
                    }
                    break;
                case 2: //载入完成
                    if (options.loadedListener) {
                        options.loadedListener.apply(req, arguments);
                    }
                    break;
                case 3: //交互
                    if (options.interactiveListener) {
                        options.interactiveListener.apply(req, arguments);
                    }
                    break;
                case 4: //已完成 
                    try {
                        if (req.status && req.status == 200) {
                            if (options.completeListener) {
                                options.completeListener.apply(req, arguments);
                            }
                        }
                        else {
                            if (options.errorListener) {
                                options.errorListener.apply(req, arguments);
                            }
                        }
                    }
                    catch (e) {

                    }
                    break;
            }
        };
        //开启请求
        req.open(options.method, url, true);
        return req;
    }
    window['ADS']['getRequest'] = getRequest;
    //AJAX请求
    function ajaxRequest(url, options) {
        var req = getRequest(url, options);
        return req.send(options.send);
    }
    window['ADS']['ajaxRequest'] = ajaxRequest;
    /*------------ADS------------------*/
    /*------------Cookie------------------*/
    var Cookies = {};
    //设置Cookies  
    Cookies.set = function (name, value) {
        var argv = arguments;
        var argc = arguments.length;
        var expires = (argc > 2) ? argv[2] : null;
        var path = (argc > 3) ? argv[3] : '/';
        var domain = (argc > 4) ? argv[4] : null;
        var secure = (argc > 5) ? argv[5] : false;
        document.cookie = name + "=" + escape(value) +
       ((expires == null) ? "" : ("; expires=" + expires.toGMTString())) +
       ((path == null) ? "" : ("; path=" + path)) +
       ((domain == null) ? "" : ("; domain=" + domain)) +
       ((secure == true) ? "; secure" : "");
    };
    //读取Cookies  
    Cookies.get = function (name) {
        var arg = name + "=";
        var alen = arg.length;
        var clen = document.cookie.length;
        var i = 0;
        var j = 0;
        while (i < clen) {
            j = i + alen;
            if (document.cookie.substring(i, j) == arg)
                return getCookieVal(j);
            i = document.cookie.indexOf(" ", i) + 1;
            if (i == 0)
                break;
        }
        return '';
    };
    //清除Cookies  
    Cookies.clear = function (name) {
        if (Cookies.get(name)) {
            var expdate = new Date();
            expdate.setTime(expdate.getTime() - (86400 * 1000 * 1));
            Cookies.set(name, "", expdate);
        }
    };

    var getCookieVal = function (offset) {
        var endstr = document.cookie.indexOf(";", offset);
        if (endstr == -1) {
            endstr = document.cookie.length;
        }
        return unescape(document.cookie.substring(offset, endstr));
    };
    window["ADS"]["Cookies"] = Cookies;
    /*------------Cookie------------------*/


    /*------------Validate-----------------*/
    var Validate = {};
    Validate = function (node) {

    }
    Validate.message= {
		required: "This field is required.",
		remote: "Please fix this field.",
		email: "Please enter a valid email address.",
		url: "Please enter a valid URL.",
		date: "Please enter a valid date.",
		dateISO: "Please enter a valid date (ISO).",
		dateDE: "Bitte geben Sie ein gültiges Datum ein.",
		number: "Please enter a valid number.",
		numberDE: "Bitte geben Sie eine Nummer ein.",
		digits: "Please enter only digits",
		creditcard: "Please enter a valid credit card number.",
		equalTo: "Please enter the same value again.",
		accept: "Please enter a value with a valid extension."
//		maxlength: $.validator.format("Please enter no more than {0} characters."),
//		minlength: $.validator.format("Please enter at least {0} characters."),
//		rangelength: $.validator.format("Please enter a value between {0} and {1} characters long."),
//		range: $.validator.format("Please enter a value between {0} and {1}."),
//		max: $.validator.format("Please enter a value less than or equal to {0}."),
//		min: $.validator.format("Please enter a value greater than or equal to {0}.")
	};
    window["ADS"]["Validate"] = Validate;
    /*------------Validate-----------------*/
})();